#!/bin/bash

function Print {
	echo -e '\E[47;30m'"\033[1m$1\033[0m"
}

function WriteVar {
	echo "$1=$2" >> $INSTALL_SDK/Install.mk
	echo "export $1" >> $INSTALL_SDK/Install.mk
}

function CheckProgram {
    Print "Checking if '$1' is installed..."
    if [ "`type -P $1`" = "" ]; then
		echo "Not installed!"
		exit 1;
	fi
}

Print "Welcome to the YoghurtGum 0.3 installation script."

CheckProgram sed
CheckProgram wget

Print "Configuration checked."

SKIP_DOWNLOAD="false"
VERSION_SDK=android-sdk_r08
VERSION_NDK=android-ndk-r5
VERSION_CRYSTAX_NDK=android-ndk-r4
VERSION_CRYSTAX=crystax-4

CHANGE=""

PS3="What would you like to install? "
OPTIONS="Android"
select opt in $OPTIONS; do
	if [ "$opt" = "Android" ]; then
		break
	else
		Print "Installation cancelled."
		exit
	fi
done

# installing the sdk and ndk

if [ "$YG_SDK" == "" ]; then
	touch ~/.bashrc

	Print "Where would you like to install the Android SDK and NDK?"
	read INSTALL_SDK
	mkdir -p $INSTALL_SDK
	
	touch $INSTALL_SDK/Install.mk
	source $INSTALL_SDK/Install.mk
	
	echo "" >> ~/.bashrc
	echo "# YoghurtGum 0.3" >> ~/.bashrc
	echo "YG_ROOT=`pwd`" >> ~/.bashrc
	echo "export YG_ROOT" >> ~/.bashrc
	echo "YG_SDK=$INSTALL_SDK" >> ~/.bashrc
	echo "export YG_SDK" >> ~/.bashrc
	echo "source \$YG_SDK/Install.mk" >> ~/.bashrc

	if [ "$SKIP_DOWNLOAD" != "true" ]; then
		Print "Getting Android SDK..."
		wget -O $INSTALL_SDK/$VERSION_SDK-linux_86.tgz http://dl.google.com/android/$VERSION_SDK-linux_86.tgz
		cd $INSTALL_SDK && tar xzpf $VERSION_SDK-linux_86.tgz && rm -f $VERSION_SDK-linux_86.tgz
		WriteVar "YG_SDK_ROOT" $INSTALL_SDK/android-sdk-linux_86
	fi
	
	crystax="false"
	Print "Please select the version of the NDK you would like to install:"
	PS3="Version: "
	select opt in \
		'Vanilla (Google supplied, safest)' \
		'Crystax (experimental, more C++ features)'
	do
		if [ "$opt" = "Vanilla (Google supplied, safest)" ]; then
			crystax="false"
			break
		else
			crystax="true"
			break
		fi
	done

	if [ "$SKIP_DOWNLOAD" != "true" ]; then
		Print "Getting Android NDK..."
		if [ "$crystax" == "true" ]; then
			wget -O $INSTALL_SDK/$VERSION_CRYSTAX_NDK-linux-x86-$VERSION_CRYSTAX.tar.bz2 http://smartctl.net/data/$VERSION_CRYSTAX_NDK-linux-x86-$VERSION_CRYSTAX.tar.bz2
			cd $INSTALL_SDK && tar xpf $VERSION_CRYSTAX_NDK-linux-x86-$VERSION_CRYSTAX.tar.bz2 && rm -f $VERSION_CRYSTAX_NDK-linux-x86-$VERSION_CRYSTAX.tar.bz2
			WriteVar "YG_NDK_ROOT" $INSTALL_SDK/$VERSION_CRYSTAX_NDK-crystax
		else
			wget -O $INSTALL_SDK/$VERSION_NDK-linux-x86.tar.bz2 http://dl.google.com/android/ndk/$VERSION_NDK-linux-x86.tar.bz2
			cd $INSTALL_SDK && tar xpf $VERSION_NDK-linux-x86.tar.bz2 && rm -f $VERSION_NDK-linux-x86.tar.bz2
			WriteVar "YG_NDK_ROOT" $INSTALL_SDK/$VERSION_NDK
			
			WriteVar "YG_OBJDUMP" $INSTALL_SDK/$VERSION_NDK/build/prebuilt/linux-x86/arm-eabi-4.2.1/arm-eabi/bin
		fi
		WriteVar "YG_CRYSTAX" $crystax
	fi

	Print "Downloading latest SDK modules..."
	$INSTALL_SDK/android-sdk-linux_86/tools/android update sdk
else
	Print "The Android development kits are installed in: $YG_SDK"
	INSTALL_SDK=$YG_SDK
	
	CHANGE='Installation directory' 
fi

if [ "$YG_PATCHED" != "True" ]; then
	Print "Installing Java..."
	sudo apt-get -y install ant sun-java6-jdk

	Print "Patching Ant..."
	sed -i s/android.libraries./project.libraries./g $INSTALL_SDK/android-sdk-linux_86/tools/ant/lib_rules.xml
	
	Print "Patching PATH variable..."
	echo "PATH=\$PATH:$INSTALL_SDK/android-sdk-linux_86/tools" >> ~/.bashrc
	echo "export PATH" >> ~/.bashrc
	
	Print "Setting up permissions..."
	chmod +x $INSTALL_SDK/android-sdk-linux_86/platform-tools/adb
	
	WriteVar "YG_PATCHED" "True"
fi

# we need the installation path for commands

PATH=$PATH:$INSTALL_SDK/android-sdk-linux_86/tools
export PATH

# installing a virtual device

if [ "$YG_VIRTUAL_DEVICE" == "" ]; then
	Print "Creating Android Virtual Device..."

	PS3="Select the target Android version: (Pick '1' if not sure) "
	select opt in \
		'Android 2.3.3 (API 10)' \
		'Android 2.3.1 (API 9)' \
		'Android 2.2 (API 8)' \
		'Android 2.1 update-1 (API 7)' \
		'Android 1.6 (API 4)' \
		'Android 1.5 (API 3)'
	do
		if [ "$opt" = "Android 1.5 (API 3)" ]; then
			AVD_VERSION="android-3"
			break
		elif [ "$opt" = "Android 1.6 (API 4)" ]; then
			AVD_VERSION="android-4"
			break
		elif [ "$opt" = "Android 2.1 update-1 (API 7)" ]; then
			AVD_VERSION="android-7"
			break
		elif [ "$opt" = "Android 2.2 (API 8)" ]; then
			AVD_VERSION="android-8"
			break
		elif [ "$opt" = "Android 2.3.1 (API 9)" ]; then
			AVD_VERSION="android-9"
			break
		elif [ "$opt" = "Android 2.3.3 (API 10)" ]; then
			AVD_VERSION="android-10"
			break
		else
			echo "Installation cancelled."
			exit
		fi
	done

	Print "Name of the AVD?"
	read AVD_NAME

	android \
	create avd \
	-n $AVD_NAME \
	-p $INSTALL_SDK/$AVD_NAME\_$AVD_VERSION \
	-t $AVD_VERSION
	
	Print "Virtual Device created: $INSTALL_SDK/$AVD_NAME_$AVD_VERSION"

	echo "" >> $INSTALL_SDK/Install.mk
	echo "# Virtual device" >> $INSTALL_SDK/Install.mk
	echo "YG_VIRTUAL_DEVICE=$AVD_NAME" >> $INSTALL_SDK/Install.mk
	echo "export YG_VIRTUAL_DEVICE" >> $INSTALL_SDK/Install.mk
	echo "YG_TARGET_VERSION=$AVD_VERSION" >> $INSTALL_SDK/Install.mk
	echo "export YG_TARGET_VERSION" >> $INSTALL_SDK/Install.mk
else
	Print "The AVD to be used is '$YG_VIRTUAL_DEVICE', which runs on '$YG_TARGET_VERSION'"
	
	CHANGE=$CHANGE:' Virtual device'
fi

# generating a keystore for signing

if [ "$YG_PASSFILE" == "" ]; then
	Print "YoghurtGum must create a pass file to be able to build projects."

	Print "Name of the keystore?"
	read KEYSTORE_NAME
	
	Print "Password?"
	read KEYSTORE_PW

	Print "Name of the alias? (Left blank it's the same as the one before)"
	read KEYSTORE_ALIAS
	if [ "$KEYSTORE_ALIAS" == "" ]; then
		KEYSTORE_ALIAS=$KEYSTORE_NAME
	fi
	
	Print "Password of the alias? (Left blank it's the same as the one before)"
	read KEYSTORE_ALIAS_PW
	if [ "$KEYSTORE_ALIAS_PW" == "" ]; then
		KEYSTORE_ALIAS_PW=$KEYSTORE_PW
	fi
	      
	keytool -genkey -v \
	-keystore $INSTALL_SDK/$KEYSTORE_NAME.keystore \
	-alias $KEYSTORE_ALIAS \
	-keyalg RSA \
	-keysize 2048 \
	-validity 10000
	
	echo "" >> $INSTALL_SDK/Install.mk
	echo "# Keystore" >> $INSTALL_SDK/Install.mk
	echo "YG_PASSFILE=$INSTALL_SDK/$KEYSTORE_NAME.keystore" >> $INSTALL_SDK/Install.mk
	echo "export YG_PASSFILE" >> $INSTALL_SDK/Install.mk
	echo "YG_PASSFILE_PW=$KEYSTORE_PW" >> $INSTALL_SDK/Install.mk
	echo "export YG_PASSFILE_PW" >> $INSTALL_SDK/Install.mk
	echo "YG_PASSFILE_ALIAS=$KEYSTORE_ALIAS" >> $INSTALL_SDK/Install.mk
	echo "export YG_PASSFILE_ALIAS" >> $INSTALL_SDK/Install.mk
	echo "YG_PASSFILE_ALIAS_PW=$KEYSTORE_ALIAS_PW" >> $INSTALL_SDK/Install.mk
	echo "export YG_PASSFILE_ALIAS_PW" >> $INSTALL_SDK/Install.mk
else
	Print "The keystore to be used is located at '$YG_PASSFILE'"
fi

Print "Installation complete."